﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace Nop.Core.Configuration
{
    public class NopConfig : IConfigurationSectionHandler
    {
        public bool DynamicDiscovery { get; private set; }
        public string EngineType { get; private set; }
        public string ThemeBasePath { get; private set; }
        public bool IgnoreStartupTasks { get; set; }
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            var config = new NopConfig();
            var dynamicDiscoveryNode = section.SelectSingleNode("DynamicDiscovery");
            if (dynamicDiscoveryNode != null && dynamicDiscoveryNode.Attributes != null)
            {
                var attribute = dynamicDiscoveryNode.Attributes["Enabled"];
                if (attribute != null)
                {
                    config.DynamicDiscovery = Convert.ToBoolean(attribute.Value);
                }
            }

            var engineNode = section.SelectSingleNode("Engine");
            if (engineNode != null && engineNode.Attributes != null)
            {
                var attribute = engineNode.Attributes["Type"];
                if (attribute != null)
                {
                    config.EngineType = attribute.Value;
                }
            }
            var startupNode = section.SelectSingleNode("Startup");
            if (startupNode != null && startupNode.Attributes != null)
            {
                var attribute = startupNode.Attributes["IgnoreStartupTasks"];
                if (attribute != null)
                {
                    config.IgnoreStartupTasks = Convert.ToBoolean(attribute.Value);
                }
            }
            var themeNode = section.SelectSingleNode("Themes");
            if (themeNode != null && themeNode.Attributes != null)
            {
                var attribute = themeNode.Attributes["basePath"];
                if (attribute != null)
                    config.ThemeBasePath = attribute.Value;
            }

            return config;

        }
    }
}
